package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Department;
import unipi.helpers.HTMLPages;
import unipi.helpers.Hash;
import unipi.helpers.LoginCookie;
import unipi.helpers.StatChartHandler;
import unipi.helpers.Validator;

public class DptStatistics extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public DptStatistics()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String dptID, HttpServletRequest request)
	{
		String content = "<h1>Στατιστικά</h1>";
		
		List<List<String>> dpts = Department.getDpts();
		
		if(dpts == null)
		{
			content += "Παρουσάστηκε σφάλμα κατα την φόρτωση των στατιστικών.";
		}
		else
		{		
			if(dptID == null) dptID = dpts.get(0).get(0);
			
			String dptName = "";
			for(List<String> row : dpts)
			{
				if(dptID.equals(row.get(0)))
				{
					dptName = row.get(1);
					break;
				}
			}
			content += "<h2>του τμήματος " + dptName + "</h2>";
			
			content += "<form name=\"fm_update\" method=\"get\" action=\"DptStatistics\"><div class=\"ddldivstyle\">" +
						HTMLPages.getDropDownList(dpts, "did", dptID, null, "onchange=\"document.fm_update.submit();\"") +
						"</div></form><br /><br />";
						
			String ppmFIleName = "";		
			String ppdFileName = "";
			String lctFileName = "";
			String lptFileName = "";
			String teamFileName = "";
			String hashedID = Hash.SHA1(dptID);
			try
			{
				ppmFIleName = StatChartHandler.getDptProgressPerMonthLineChart(dptID, hashedID, request);
				ppdFileName = StatChartHandler.getDptProgressPerDayBarChart(dptID, hashedID, request);
				lctFileName = StatChartHandler.getDptLateCompletedPieChart(dptID, hashedID, request);
				lptFileName = StatChartHandler.getDptLatePendingPieChart(dptID, hashedID, request);
				teamFileName = StatChartHandler.getDptTaskTeamsPieChart(dptID, hashedID, request);
			}
			catch(IOException io)
			{
				System.out.println("IOException on Chart creation : " + io.toString());
			}
			
			content += "<table><tr>";			
			if(teamFileName != null) content += "<td><img src=\"" + teamFileName + "\" alt=\"Chart\" /></td>";
			if(ppmFIleName != null) content += "<td><img src=\"" + ppmFIleName + "\" alt=\"Chart\" /></td>";
			content += "</tr><tr>";
			if(ppdFileName != null) content += "<td colspan=\"2\"><img src=\"" + ppdFileName + "\" alt=\"Chart\" /></td>";
			content += "</tr><tr>";
			if(lptFileName != null) content += "<td><img src=\"" + lptFileName + "\" alt=\"Chart\" /></td>";
			if(lctFileName != null) content += "<td><img src=\"" + lctFileName + "\" alt=\"Chart\" /></td>";
			content += "</tr></table>";
		
		}
				
		return HTMLPages.getGeneralPage(content, uid);	
	}
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String dptID = request.getParameter("did");
			if(!Validator.isNumber(dptID)) dptID = null;
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, dptID, request));
			
			out.close();			
		}
	}
	
}
